//
//  DiscoverViewController.m
//  陶瓷
//
//  Created by 郭天龙 on 15/7/6.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverSecondViewController.h"
#import "UIColor+NSString.h"
#import "UIImageView+WebCache.h"
#import "CompanyDisplayViewController.h"

@interface DiscoverViewController ()

@property (nonatomic, strong) IBOutlet UITableView *tableview;
@property (nonatomic, strong) NSArray *arrayDataSource;
@property (nonatomic, strong) NSDictionary *adverData;

@end

@implementation DiscoverViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        _nextVC = NO;
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arrayDataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"discoverCell"];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"discoverCell"];
    UIImageView *imagev = [[UIImageView alloc] init];
    imagev.frame = CGRectMake(0, 0, 5, 50);
    imagev.backgroundColor = [UIColor colorWithString:[[self.arrayDataSource objectAtIndex:indexPath.row] objectForKey:@"color"]];
    [cell.contentView addSubview:imagev];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = [[self.arrayDataSource objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
    cell.detailTextLabel.text = [[self.arrayDataSource objectAtIndex:indexPath.row] objectForKey:@"detail"];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationItem setTitle:@""];
    switch (indexPath.row) {
        case 0:
        {
            [self gotoDiscoverSecondViewController:0 andAnimated:YES];
        }
            break;
        case 1:
        {
            [self gotoDiscoverSecondViewController:1 andAnimated:YES];
        }
            break;
        case 2:
        {
            [self gotoDiscoverSecondViewController:2 andAnimated:YES];
        }
            break;
        case 3:
        {
            TCLog(@"3");
        }
            break;
        case 4:
        {
            TCLog(@"4");
        }
            break;
            
        default:
            break;
    }
}


- (void)gotoDiscoverSecondViewController:(int)type andAnimated:(BOOL)animated
{
    DiscoverSecondViewController *discover = [[DiscoverSecondViewController alloc] init];
    discover.tableType = type;
    [self.navigationController pushViewController:discover animated:YES];
}


#pragma mark - lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.title = @"发现更多";
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"discoverCell"];
    self.tableview.tableFooterView = [UIView new];
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, kDefaultWindowWidth, kDefaultWindowWidth/2);
    self.tableview.tableHeaderView = imageView;
    self.arrayDataSource = [[NSArray alloc] initWithObjects:@{@"title":@"人才招聘",@"detail":@"精英级人才聚集地",@"color":@"#10B2CE"},@{@"title":@"转让招租",@"detail":@"仓库店面转让出租",@"color":@"#1033CE"},@{@"title":@"市场分布",@"detail":@"本市所有市场分布",@"color":@"#8E33CE"},@{@"title":@"物流配送",@"detail":@"帮您快速选择物流配送",@"color":@"#8EC733"},@{@"title":@"游戏中心",@"detail":@"暂未开放",@"color":@"#07C733"}, nil];
    
    TCNetworkTool *net = [TCNetworkTool sharedNetTool];
    [net getHomeAdvertiseSuccessBlocks:^(NSDictionary *resultDic) {
        if ([[resultDic objectForKey:@"FindMore"] count] > 0) {
            _adverData = [[[resultDic objectForKey:@"FindMore"] objectAtIndex:0] mutableCopy];
            [imageView sd_setImageWithURL:[NSURL URLWithString:[_adverData objectForKey:@"ImageUrl"]] placeholderImage:nil];
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adver)];
            [imageView addGestureRecognizer:gesture];
        }
    } andFailureBlocks:^(NSString *failureMessage) {
//        [MBHUDHelper showWarningWithText:failureMessage];
    }];
    
}

- (void)adver
{
    TCNetworkTool *tctool = [TCNetworkTool sharedNetTool];
    [tctool GetCompanyWithID:[self.adverData objectForKey:@"TargetId"] andSuccessBlocks:^(NSArray *resultArray) {
        CompanyDisplayViewController *nextController = [[CompanyDisplayViewController alloc] init];
        self.title = @"";
        nextController.company = resultArray.firstObject;
        [self.navigationController pushViewController:nextController animated:YES];
    } andFailureBlocks:^(NSString *failureMessage) {
        [MBHUDHelper showWarningWithText:failureMessage];
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"发现更多"];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (self.nextVC) {
        [self gotoDiscoverSecondViewController:self.nextNum andAnimated:NO];
        self.nextVC = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
